package signIn;

import coupon.DiscountCoupon;
import enums.CustomActivityStatus;
import models.BaseModel;
import models.admin.AdminUser;
import models.constants.DeletedStatus;
import models.merchant.Merchant;

import javax.persistence.*;
import java.util.Date;
import java.util.List;

/**
 * 签到表
 * Created by liuBin on 2017/8/5.
 */
@Entity
@Table(name = "sign_in")
public class SignIn  extends BaseModel {

    /**
     * 活动编号
     */
    @Column(name = "code")
    public String code;

    /**
     * 活动标题
     */
    @Column(name = "title")
    public String title;

    /**
     * 商户
     */
    @ManyToOne
    @JoinColumn(name = "merchant_id")
    public Merchant merchant;

    /**
     * 代金券
     */
    @ManyToOne
    @JoinColumn(name = "discount_coupon")
    public DiscountCoupon discountCoupon;

    /**
     * 排序号
     */
    @Column(name = "show_order")
    public Long showOrder;

    /**
     * 活动开始时间
     */
    @Column(name = "begin_date")
    @Temporal(TemporalType.TIMESTAMP)
    public Date beginDate;

    @Transient
    public String beginDateStr;


    /**
     * 活动结束时间
     */
    @Column(name = "end_date")
    @Temporal(TemporalType.TIMESTAMP)
    public Date endDate;

    @Transient
    public String endDateStr;


    /**
     * 活动状态
     */
    @Column(name = "custom_activity_status")
    @Enumerated(EnumType.STRING)
    public CustomActivityStatus customActivityStatus;

    /**
     * 活动开启人
     */
    @ManyToOne
    @JoinColumn(name = "start_user_id")
    public AdminUser startUser;

    /**
     * 开启时间
     */
    @Column(name = "start_date")
    @Temporal(TemporalType.TIMESTAMP)
    public Date startDate;

    @Transient
    public String startDateStr;

    /**
     * 强制终止人
     */
    @ManyToOne
    @JoinColumn(name = "abort_user_id")
    public AdminUser abortUser;

    /**
     * 强制终止时间
     */
    @Column(name = "abort_date")
    @Temporal(TemporalType.TIMESTAMP)
    public Date abortDate;

    @Transient
    public String abortDateStr;

    /**
     * 备注
     */
    @Lob
    @Column(name = "remark")
    public String remark;


    /**
     * 签到天数
     */
    @Column(name = "days")
    public Integer days;


    /**
     * 版本号
     */
    @Version
    public Integer version;


    /**
     * 逻辑删除状态
     */
    @Column(name = "deleted")
    @Enumerated(EnumType.ORDINAL)
    public DeletedStatus deleted;

    public SignIn() {super();}


    /**
     *
     * @param id
     * @return
     */
    public static SignIn findBySignInId(long id){
        return SignIn.find(" id = ? and deleted = ?" , id , DeletedStatus.UN_DELETED).first();
    }

    /**
     * 查询商户正在进行的签到
     * @param merchant
     * @return
     */
    public static List<SignIn> findSignInInTime(Merchant merchant ){
        Date nowTime = new Date();
        return SignIn.find("merchant = ? and deleted = ? and beginDate <= ? and endDate >= ? and customActivityStatus = ? order by days desc" ,
                merchant , DeletedStatus.UN_DELETED,nowTime , nowTime , CustomActivityStatus.START).fetch();
    }
}
